package com.hdoit.ares.web.insu.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.insu.model.Insurance;
import com.hdoit.ares.logical.insu.model.InsuranceCompany;
import com.hdoit.ares.logical.insu.model.InsurancePeople;
import com.hdoit.ares.logical.insu.service.IInsuranceCompanyService;
import com.hdoit.ares.logical.insu.service.IInsurancePeopleService;
import com.hdoit.ares.logical.insu.service.IInsuranceService;
import com.hdoit.ares.logical.viewmodel.insu.FindInsu;
import com.tennetcn.common.logical.file.model.OssFileInfo;
import com.tennetcn.common.logical.file.service.IOssFileInfoService;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;
import com.tennetcn.web.cbase.ApiAuthPassport;

@Controller
@RequestMapping(value = "/api/v1/InsuranceAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/InsuranceAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class InsuranceAresApi extends BaseApi{

	private IInsuranceService insuranceService;
	private IInsuranceCompanyService companyService;
	private IInsurancePeopleService peopleService;
	private IOssFileInfoService fileInfoService;
	private IWithdrawParamService withdrawParamService;

	
	public IInsuranceService getInsuranceService() {
		return insuranceService;
	}
	@Resource
	public void setInsuranceService(IInsuranceService insuranceService) {
		this.insuranceService = insuranceService;
	}
	
	public IInsuranceCompanyService getCompanyService() {
		return companyService;
	}	
	@Resource
	public void setCompanyService(IInsuranceCompanyService companyService) {
		this.companyService = companyService;
	}
	
	public IInsurancePeopleService getPeopleService() {
		return peopleService;
	}
	@Resource
	public void setPeopleService(IInsurancePeopleService peopleService) {
		this.peopleService = peopleService;
	}
	
	public IOssFileInfoService getFileInfoService() {
		return fileInfoService;
	}
	@Resource
	public void setFileInfoService(IOssFileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String showInsuApi = "showInsu";
	private final String showHotInsuApi = "showHotInsu";
	private final String insulistApi = "insulist";
	private final String toInsuDetailApi = "toInsuDetail";
	private final String jingXuanApi = "jingXuan";

	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = jingXuanApi)
	@ApiOperation(notes= jingXuanApi,httpMethod = "POST" ,value = "首页精选产品")
	public String jingXuan(){
		
		/*真实的图片路径前缀*/
		String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
		
		/*测试的图片路径前缀*/
		/*String preUrl = "http://test-1254043822.cosgz.myqcloud.com";*/
		
		ResponseModel rm = new ResponseModel();
		
		FindInsu findInsu1 = new FindInsu();
		findInsu1.setStatus("chooseProduct1");
		List<Insurance> product1 = insuranceService.queryListByFindInsu(findInsu1);
		if(product1.size()>0){
			Insurance chooseProduct1=product1.get(0);
			if(!StringUtils.isEmpty(chooseProduct1.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(chooseProduct1.getAddImg());
				chooseProduct1.setAddImg(preUrl+ossFileInfo.getFilename());
				
				
			}
			rm.put("chooseProduct1",chooseProduct1);
		}else{
			System.err.println("精选产品1为空");
		}	
		
		FindInsu findInsu2 = new FindInsu();
		findInsu2.setStatus("chooseProduct2");
		List<Insurance> product2 = insuranceService.queryListByFindInsu(findInsu2);
		if(product2.size()>0){
			Insurance chooseProduct2=product2.get(0);
			if(!StringUtils.isEmpty(chooseProduct2.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(chooseProduct2.getAddImg());
				chooseProduct2.setAddImg(preUrl+ossFileInfo.getFilename());
			}
			rm.put("chooseProduct2",chooseProduct2);
		}else{
			System.err.println("精选产品2为空");
		}	
		
		FindInsu findInsu3 = new FindInsu();
		findInsu3.setStatus("chooseProduct3");
		List<Insurance> product3 = insuranceService.queryListByFindInsu(findInsu3);
		if(product3.size()>0){
			Insurance chooseProduct3=product3.get(0);
			if(!StringUtils.isEmpty(chooseProduct3.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(chooseProduct3.getAddImg());
				chooseProduct3.setAddImg(preUrl+ossFileInfo.getFilename());
			}
			rm.put("chooseProduct3",chooseProduct3);
		}else{
			System.err.println("精选产品3为空");
		}	
		
		FindInsu findInsu4 = new FindInsu();
		findInsu4.setStatus("chooseProduct4");
		List<Insurance> product4 = insuranceService.queryListByFindInsu(findInsu4);
		if(product4.size()>0){
			Insurance chooseProduct4=product4.get(0);
			if(!StringUtils.isEmpty(chooseProduct4.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(chooseProduct4.getAddImg());
				chooseProduct4.setAddImg(preUrl+ossFileInfo.getFilename());
			}
			rm.put("chooseProduct4",chooseProduct4);
		}else{
			System.err.println("精选产品4为空");
		}	
		
		return rm.toJson();
		}
		
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = showHotInsuApi)
	@ApiOperation(notes= showHotInsuApi,httpMethod = "POST" ,value = "首页热卖产品")
	public String hotSort(){
		ResponseModel rm = new ResponseModel();
		FindInsu findInsu = new FindInsu();
		findInsu.setStatus("hotInsurance");
		findInsu.setOrder("hotSortCode");
		findInsu.setUpOrDown("asc");
		findInsu.setPageNum(1);
		
		List<Insurance> insurances = insuranceService.queryListByFindInsu(findInsu);
		
		String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
		
		for (Insurance insurance : insurances) {
			if(!StringUtils.isEmpty(insurance.getLogoUrl())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getLogoUrl());
				insurance.setCompanyUrl(preUrl+ossFileInfo.getFilename());
			}
			if(!StringUtils.isEmpty(insurance.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getAddImg());
				insurance.setInsuAddUrl(preUrl+ossFileInfo.getFilename());
			}
			if(!StringUtils.isEmpty(insurance.getMainImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getMainImg());
				insurance.setInsuMainUrl(preUrl+ossFileInfo.getFilename());
			}
		}
		
		if(insurances.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = Insurance.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("insulist", insurances, params);
		}
		
		return rm.toJson();
	}
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = showInsuApi)
	@ApiOperation(notes= showInsuApi,httpMethod = "POST" ,value = "进入保险展示页")
	public String showInsu(){
		/*@ApiParam("一级险种(必填)") @RequestParam(value="insuOne",required=true)String insuOne*/
		
		ResponseModel rm = new ResponseModel();
		FindInsu findInsu = new FindInsu();

		//查意外险的二级菜单
		List<ParamOption> healthOptions = insuranceService.queryList("3533ef78-b68b-42dd-a495-52b814efa33f");
		List<ParamOption> moneyOptions = insuranceService.queryList("72edc2e8-cde4-403d-bee7-14353d844394");
		List<ParamOption> accOptions = insuranceService.queryList("1ff92d07-d3bb-45df-9104-4708917958bb");
		
		//查意外险的二级菜单
		List<InsuranceCompany> companys = companyService.queryList();
		List<InsurancePeople> peoples = peopleService.queryList();
		
		//总页数
		WithdrawParam pageSize = withdrawParamService.queryModelByHelpCode("pageSize");	
		List<Insurance> insurances = new ArrayList<Insurance>();
		findInsu.setInsuOne("");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int allPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("意外险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int accPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("健康险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int healthPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("财产险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int MoneyPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
				
		List<String> optionsParams = new ArrayList<String>();
		Field[] fields = ParamOption.class.getDeclaredFields();
		for(Field field : fields){
			optionsParams.add(field.getName());
		}
				
		List<String> companysParams = new ArrayList<String>();
		Field[] companysfields = InsuranceCompany.class.getDeclaredFields();
		for(Field field : companysfields){
			companysParams.add(field.getName());
		}
				
		List<String> peoplesParams = new ArrayList<String>();
		Field[] peoplesfields = InsurancePeople.class.getDeclaredFields();
		for(Field field : peoplesfields){
			peoplesParams.add(field.getName());
		}
				
		
		rm.putList("healthOptions", healthOptions, optionsParams);
		rm.putList("moneyOptions", moneyOptions, optionsParams);
		rm.putList("accOptions", accOptions, optionsParams);
		
		rm.putList("peoples", peoples, peoplesParams);
		rm.putList("companys", companys, companysParams);
		
		rm.put("allPage", allPage);
		rm.put("accPage", accPage);
		rm.put("healthPage", healthPage);
		rm.put("MoneyPage", MoneyPage);
		
		return rm.toJson();
	}
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = insulistApi)
	@ApiOperation(notes= insulistApi,httpMethod = "POST" ,value = "保险列表")
	public String insulist(FindInsu findInsu){
		
		ResponseModel rm = new ResponseModel();
		List<Insurance> insurances = insuranceService.queryListByFindInsu(findInsu);

		
		String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";		
		for (Insurance insurance : insurances) {
			if(!StringUtils.isEmpty(insurance.getLogoUrl())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getLogoUrl());
				insurance.setCompanyUrl(preUrl+ossFileInfo.getFilename());
			}
			if(!StringUtils.isEmpty(insurance.getAddImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getAddImg());
				insurance.setInsuAddUrl(preUrl+ossFileInfo.getFilename());
			}
			if(!StringUtils.isEmpty(insurance.getMainImg())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(insurance.getMainImg());
				insurance.setInsuMainUrl(preUrl+ossFileInfo.getFilename());
			}
		}
		
		if(insurances.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = Insurance.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("insurances", insurances, params);
		}

		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = toInsuDetailApi)
	@ApiOperation(notes= toInsuDetailApi,httpMethod = "POST" ,value = "进入保险详情")
	public String toInsuDetail(){
		
		ResponseModel rm = new ResponseModel();

		return rm.toJson();
	}


	
}
